package com.longdaji.decoration.ui.goodslist;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.base.BaseFragment;
import com.longdaji.decoration.ui.goodslist.select.GoodsSelectFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by longtao on 2017/12/19.
 */

public class GoodsListActivity extends BaseActivity {

    private static final String TAG = GoodsListActivity.class.getSimpleName();
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tlyt_goods_list_select)
    TabLayout mTlytGoodsListSelect;
    @BindView(R.id.viewpager_goods_list)
    ViewPager mViewPagerGoodsList;
    private List<BaseFragment> mFragments;
    private GoodsListSelectTabAdapter mAdapter;
    private String[] mTitles = {"全部", "最新", "最热"};
    private String mCateId;


    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, GoodsListActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "GoodsListActivity onCreate");
        setContentView(R.layout.activity_goods_list);
        setUnBinder(ButterKnife.bind(this));
        if (getIntent() != null) {
            mTvTitle.setText(getIntent().getStringExtra("category_name"));
            mCateId = getIntent().getStringExtra("cate_id");
            Log.d(TAG, "GoodsListActivity mCateId=" + mCateId);
        }
        setUp();
    }


    private void setUp() {
        mFragments = new ArrayList<>();
        Bundle bundleAll = new Bundle();
        bundleAll.putString("cate_id", mCateId);
        bundleAll.putString("order_by", "all");
        GoodsSelectFragment mAllFragment = GoodsSelectFragment.newInstance(bundleAll);
        Bundle bundleNew = new Bundle();
        bundleNew.putString("cate_id", mCateId);
        bundleNew.putString("order_by", "new");
        GoodsSelectFragment mNewFragment = GoodsSelectFragment.newInstance(bundleNew);
        Bundle bundleHot = new Bundle();
        bundleHot.putString("cate_id", mCateId);
        bundleHot.putString("order_by", "hot");
        GoodsSelectFragment mHotFragment = GoodsSelectFragment.newInstance(bundleHot);
        mFragments.add(mAllFragment);
        mFragments.add(mNewFragment);
        mFragments.add(mHotFragment);
        //ViewPager的适配器
        mAdapter = new GoodsListSelectTabAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mViewPagerGoodsList.setAdapter(mAdapter);
        mViewPagerGoodsList.setOffscreenPageLimit(4);
        //绑定
        mTlytGoodsListSelect.setupWithViewPager(mViewPagerGoodsList);
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }
}
